﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.DataSourcesFile;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Carto;
using System.Collections;
using System.Runtime.InteropServices;
using PlanningGIS.Win.Controls;
using PlanningGIS.Framework;


namespace PlanningGIS.SelectQuery
{
   
    /// <summary>
    /// 图幅定位界面
    /// </summary>
    public partial class FrmTufuLocate : Office2007Form
    {
        private IMapControl3 m_MapControl;
        private IFeatureClass m_FeatureClass;
        IQueryFilter pFilter;
        public FrmTufuLocate()
        {
            InitializeComponent();
             pFilter = new QueryFilterClass();
        }

        /// <summary>
        /// 地图控件
        /// </summary>
        public IMapControl3 MapControl
        {
            set
            {
                m_MapControl = value;
            }
            get
            {
                return m_MapControl;
            }
        }

        private void FrmTufuLocate_Load(object sender, EventArgs e)
        {
            for (int i = 0; i < JHBHelper.ScaleValues.Count; i++)
            {
                this.cbxScale.Items.Add(JHBHelper.ScaleValues[i].ToString());
            }
            this.cbxScale.SelectedIndex = 0;
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        /// <summary>
        /// 定位图幅
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnOK_Click(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count != 1) return;
            if (m_MapControl == null) return;
            IFeatureLayer pFeatureLayer = GetTufuLayer();
            if (pFeatureLayer == null)
            {
                pFeatureLayer = new FeatureLayerClass();
                pFeatureLayer.FeatureClass = m_FeatureClass;
                pFeatureLayer.Name = ((IDataset)m_FeatureClass).Name;
                m_MapControl.AddLayer(pFeatureLayer, 0);
            }
            IFeatureSelection pFSelection = pFeatureLayer as IFeatureSelection;
            pFSelection.Clear();
            string strTfbh = listView1.SelectedItems[0].SubItems[1].Text;

            IQueryFilter filter = new QueryFilterClass();
            filter.WhereClause =  "图号='" + strTfbh + "'";
            IFeatureCursor pFcursor = m_FeatureClass.Search(filter, false);
            if (pFcursor == null) return;
            IFeature pFeature = pFcursor.NextFeature();
            System.Runtime.InteropServices.Marshal.ReleaseComObject(pFcursor);
            if (pFeature != null)
            {
                IEnvelope pEnv = pFeature.Shape.Envelope;
                pEnv.Expand(1.5, 1.5, true);
                m_MapControl.Extent = pEnv;
                pFSelection.SelectionSet.Add(pFeature.OID);
            }
            m_MapControl.ActiveView.Refresh();
        }


        /// <summary>
        /// 根据图幅号查询图幅
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnQueryTufu_Click(object sender, EventArgs e)
        {
            try
            {
                this.Cursor = Cursors.WaitCursor;
                if (m_FeatureClass == null) return;
                string strTFBH_FLD = "图号";
                listView1.Items.Clear();
                ITable pTable = m_FeatureClass as ITable;
                int fld_index = pTable.Fields.FindField(strTFBH_FLD);
                if (fld_index < 0) return;
                pFilter = new QueryFilterClass();
                //IQueryFilter pFilter = null;
                if (txtTfubh.Text.Trim() != "")
                {
                    //pFilter = new QueryFilterClass();
                    pFilter.SubFields = strTFBH_FLD;
                    pFilter.WhereClause = strTFBH_FLD + " like '%" + txtTfubh.Text.Trim() + "%'";
                }
                ICursor pCursor = pTable.Search(pFilter, false);
                int countPerPage = 100;
                int totalCount = pTable.RowCount(pFilter);
                int pageCount = totalCount % 100 == 0 ? totalCount / 100 : totalCount / 100 + 1;
                lblTotal.Text = "共" + pageCount + "页";
                txtPageIndex.Text = totalCount > 0 ? "1" : "0";
                btnNext.Enabled = pageCount > 1;
                btnPre.Enabled = false;
                IRow pRow = pCursor.NextRow();
                object obj;
                string strTfbh;
                //ArrayList m_List = new ArrayList();
                int index=0;
                while (pRow != null & index<countPerPage )
                {
                    index++;
                    obj = pRow.get_Value(fld_index);
                    strTfbh = obj == DBNull.Value ? "" : obj.ToString();
                    ListViewItem pListItem= listView1.Items.Add(index.ToString());
                    pListItem.SubItems.Add(strTfbh);
                    pRow = pCursor.NextRow();
                }

                Marshal.ReleaseComObject(pCursor);
                //for (int i = 0; i < m_List.Count; i++)
                //{
                //    listView1.Items.Add(i.ToString()).SubItems.Add(m_List[i].ToString());
                //}
            }
            catch (System.Exception)
            {
            }
            finally
            {
                this.Cursor = Cursors.Default;
            }
        }

        private void txtTfubh_KeyDown(object sender, KeyEventArgs e)
        {
           
        }

        private void listView1_DoubleClick(object sender, EventArgs e)
        {
            btnOK_Click(null, null);
        }

        private void cbxScale_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                listView1.Items.Clear();
                string scale = cbxScale.Text;
                foreach (IFeatureClass fcls in JHBHelper.FeatureClasses)
                {
                    string fclsName = ((IDataset)fcls).Name;
                    if (scale == fclsName.Replace("结合表", "").Trim())
                    {
                        m_FeatureClass = fcls;
                        break;
                    }
                }
               
            }
            catch (Exception ex)
            {
                MsgHelper.ShowErrorMsgbox(ex.Message);
            }
        }

        /// <summary>
        /// 获取图幅结合表图层
        /// </summary>
        /// <returns></returns>
        private IFeatureLayer GetTufuLayer()
        {
            ILayer pLayer;
            for (int i = 0; i < m_MapControl.LayerCount; i++)
            {
                pLayer = m_MapControl.get_Layer(i);
                if (pLayer.Valid)
                {
                    if (pLayer is IFeatureLayer && ((IFeatureLayer)pLayer).FeatureClass == m_FeatureClass)
                    {
                        return pLayer as IFeatureLayer;
                    }
                    else if (pLayer is ICompositeLayer)
                    {
                        IFeatureLayer pFlayer = GetTufuLayer(pLayer as ICompositeLayer);
                        if (pFlayer != null) return pFlayer;

                    }
                }
            }
            return null;
        }

        /// <summary>
        /// 获取图幅结合表图层
        /// </summary>
        /// <returns></returns>
        private IFeatureLayer GetTufuLayer(ICompositeLayer pGroupLayer)
        {
            ILayer pLayer;
            for (int i = 0; i < pGroupLayer.Count; i++)
            {
                pLayer = pGroupLayer.get_Layer(i);
                if (pLayer.Valid)
                {
                    if (pLayer is IFeatureLayer && ((IFeatureLayer)pLayer).FeatureClass == m_FeatureClass)
                    {
                        return pLayer as IFeatureLayer;
                    }
                    else if (pLayer is ICompositeLayer)
                    {
                        IFeatureLayer pFlayer = GetTufuLayer(pLayer as ICompositeLayer);
                        if (pFlayer != null) return pFlayer;

                    }
                }
            }
            return null;
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            ITable pTable = m_FeatureClass as ITable;

            string strTFBH_FLD = "图号";
            listView1.Items.Clear();         
            int fld_index = pTable.Fields.FindField(strTFBH_FLD);
            if (fld_index < 0) return;

            int countPerPage = 100;
            int totalCount = pTable.RowCount(pFilter);
            int pageCount = totalCount % 100 == 0 ? totalCount / 100 : totalCount / 100 + 1;

            int currentPage = Convert.ToInt32(txtPageIndex.Text)+1;
            if (currentPage > pageCount) return;
            int from = (currentPage - 1) * countPerPage + 1;
            int to = currentPage * countPerPage ;
            ICursor  pCursor= pTable.Search(pFilter, false);
            IRow pRow=pCursor.NextRow();
            int index = 1;
            while (pRow != null &  index<=to  )
            {
                if (index >= from)
                {
                    object obj = pRow.get_Value(fld_index);
                    string  strTfbh = obj == DBNull.Value ? "" : obj.ToString();
                    ListViewItem pListItem = listView1.Items.Add(index.ToString());
                    pListItem.SubItems.Add(strTfbh);
                }

                index++;
                pRow = pCursor.NextRow();
            }
            System.Runtime.InteropServices.Marshal.ReleaseComObject(pCursor);
            txtPageIndex.Text = currentPage.ToString();
            btnNext.Enabled = currentPage < pageCount;
            btnPre.Enabled = currentPage > 1;
        }

        private void btnPre_Click(object sender, EventArgs e)
        {
            ITable pTable = m_FeatureClass as ITable;

            string strTFBH_FLD = "图号";
            listView1.Items.Clear();
            int fld_index = pTable.Fields.FindField(strTFBH_FLD);
            if (fld_index < 0) return;

            int countPerPage = 100;
            int totalCount = pTable.RowCount(pFilter);
            int pageCount = totalCount % 100 == 0 ? totalCount / 100 : totalCount / 100 + 1;


            int currentPage = Convert.ToInt32(txtPageIndex.Text)-1 ;
            if (currentPage <= 0) return;                     
            int from = (currentPage - 1) * countPerPage + 1;
            int to = currentPage * countPerPage;
            ICursor pCursor = pTable.Search(pFilter, false);
            IRow pRow = pCursor.NextRow();
            int index = 1;
            while (pRow != null & index <= to)
            {
                if (index >= from)
                {
                    object obj = pRow.get_Value(fld_index);
                    string strTfbh = obj == DBNull.Value ? "" : obj.ToString();
                    ListViewItem pListItem = listView1.Items.Add(index.ToString());
                    pListItem.SubItems.Add(strTfbh);
                }

                index++;
                pRow = pCursor.NextRow();
            }
            System.Runtime.InteropServices.Marshal.ReleaseComObject(pCursor);
            txtPageIndex.Text = currentPage.ToString();
            btnNext.Enabled = currentPage <= pageCount;
            btnPre.Enabled = currentPage > 1;
        }
    }
}
